package com.ctgu.j08.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.mapper.BuyListMapper;
import com.ctgu.j08.mapper.DepartmentMapper;
import com.ctgu.j08.mapper.DeviceMapper;
import com.ctgu.j08.service.BuyListService;
import com.ctgu.j08.service.DepartmentService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.BuyList;
import com.ctgu.j08.vo.Department;
import com.ctgu.j08.vo.Device;
import com.ctgu.j08.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/6/2 9:19
 */
@Service
public class BuyListServiceImpl extends ServiceImpl<BuyListMapper, BuyList> implements BuyListService {
    @Autowired
    private BuyListMapper buyListMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public BuyList getById(int buyId) {
        BuyList buyList = buyListMapper.selectByPrimaryKey(buyId);
        return buyList;
    }

    @Override
    public List<BuyList> getByDeName(String deName) {
        List<BuyList> buyLists = buyListMapper.getByDeviceName(deName);
        return buyLists;
    }

    @Override
    public Result editBuyList(Integer buyId) {
        BuyList buyList = buyListMapper.selectByPrimaryKey(buyId);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date buyTime = new Date(System.currentTimeMillis());
        System.out.println("当前的时间："+formatter.format(buyTime));
        int num = buyList.getNum();
        int isEdit = buyListMapper.updateByBuyTime(buyId,buyTime);
        for (int i = 1; i <= num; i++) {
            Device device = new Device();
            device.setDeName(buyList.getDeName());
            device.setDeTypeid(buyList.getTypeId());
            device.setDeFacid(buyList.getFacId());
            device.setDeMoney(buyList.getMoney());
            device.setDeDate(buyTime);
            device.setDeStaffid(buyList.getUserId());
            deviceMapper.insertSelective(device);
        }
        if(isEdit != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            System.out.println("更新失败"+isEdit);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

//    @Override
//    public Result editBuyList(BuyList buyList) {
//        int buyId = buyList.getBuyId();
//        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date buyTime = new Date(System.currentTimeMillis());
//        System.out.println(formatter.format(buyTime));
////        String buyTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
////        System.out.println("当前的时间："+buyTime);
//        int num = buyList.getNum();
//        int isEdit = buyListMapper.updateByBuyTime(buyId,buyTime);
//        //批量插入
//        for (int i = 1; i <= num; i++) {
//            Device device = new Device();
//            device.setDeName(buyList.getDeName());
//            device.setDeTypeid(buyList.getTypeId());
//            device.setDeFacid(buyList.getFacId());
//            device.setDeMoney(buyList.getMoney());
//            device.setDeDate(buyTime);
//            device.setDeStaffid(buyList.getUserId());
//            deviceMapper.insertSelective(device);
//        }
//        if(isEdit != 0)
//        {
//            return ResultUtil.success(ResultEnum.SUCCESS);
//        }else
//        {
//            System.out.println("更新失败"+isEdit);
//            return ResultUtil.error(ResultEnum.FAIL);
//        }
//    }


    @Override
    public Result addBuyList(BuyList buyList) {
        int isAdd = buyListMapper.insertSelective(buyList);
        if(isAdd != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else {
            System.out.println("插入失败" + isAdd);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllBuyList() {
        List<BuyList> buyLists = buyListMapper.getAllBuyList();
        if(buyLists == null)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }else
        {
            System.out.println("采购清单："+buyLists);
            return ResultUtil.success(buyLists);
        }
    }

    @Override
    public Result searchBuyListByName(String deName) {

        List<BuyList> buyLists= buyListMapper.getByDeviceName(deName);
        if(buyLists != null)
        {
            System.out.println("搜索到的采购信息"+buyLists);
            return ResultUtil.success(buyLists);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }
}
